package fr.umlv.m2.jee.pmlv.service.planning.impl;

import fr.umlv.m2.jee.pmlv.persistence.planning.RecordDate;
import fr.umlv.m2.jee.pmlv.persistence.planning.dao.IRecordDateDAO;
import fr.umlv.m2.jee.pmlv.service.planning.DisplayRecordDate;
import fr.umlv.m2.jee.pmlv.service.planning.IDisplayRecordDateService;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Stephanie
 */
@Service("defautDisplayRecordDateService")
public class DefautDisplayRecordDateService implements IDisplayRecordDateService {

    @Autowired
    @Qualifier("hibernateRecordDateDAO")
    private IRecordDateDAO dao;

    @Override
    @Transactional(readOnly=true)
    public List<DisplayRecordDate> getAllDisplayRecordDate() {
        ArrayList< DisplayRecordDate > result = new ArrayList< DisplayRecordDate >(16);
        for(RecordDate event : dao.findAll() ) {
            result.add(new DisplayRecordDate(event));
        }
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public List<DisplayRecordDate> getRecordDateByWeekAndByProfessorUidAndByYear(GregorianCalendar start, String uid, int year) {
        ArrayList< DisplayRecordDate > result = new ArrayList< DisplayRecordDate >(16);
        for(RecordDate event : dao.findByWeekAndByProfessorUidAndByYear(start, uid, year)) {
            result.add(new DisplayRecordDate(event));
        }
        Collections.sort(result, new Comparator<DisplayRecordDate>() {
            @Override
            public int compare(DisplayRecordDate o1, DisplayRecordDate o2) {
                return o1.getDeadLine().compareTo(o2.getDeadLine());
            }
        });
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public List<DisplayRecordDate> getRecordDateByWeekAndByStudentUidAndByYear(GregorianCalendar start, String uid, int year) {
        ArrayList< DisplayRecordDate > result = new ArrayList< DisplayRecordDate >(16);
        for(RecordDate event : dao.findByWeekAndByStudentUidAndByYear(start, uid, year)) {
            result.add(new DisplayRecordDate(event));
        }
        Collections.sort(result, new Comparator<DisplayRecordDate>() {
            @Override
            public int compare(DisplayRecordDate o1, DisplayRecordDate o2) {
                return o1.getDeadLine().compareTo(o2.getDeadLine());
            }
        });
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public List<DisplayRecordDate> getRecordDateByWeekAndBySecretaryUidAndByYear(GregorianCalendar start, String uid, int year) {
        ArrayList< DisplayRecordDate > result = new ArrayList< DisplayRecordDate >(16);
        for(RecordDate event : dao.findByWeekAndBySecretaryUidAndByYear(start, uid, year)) {
            result.add(new DisplayRecordDate(event));
        }
        Collections.sort(result, new Comparator<DisplayRecordDate>() {
            @Override
            public int compare(DisplayRecordDate o1, DisplayRecordDate o2) {
                return o1.getDeadLine().compareTo(o2.getDeadLine());
            }
        });
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public void addRecordDate(DisplayRecordDate date) {
        dao.persist(date.getRecordDate());
    }

    @Override
    @Transactional(readOnly=true)
    public void editRecordDate(DisplayRecordDate date) {
        dao.merge(date.getRecordDate());
    }

    @Override
    @Transactional(readOnly=true)
    public void deleteRecordDate(DisplayRecordDate date) {
        dao.remove( dao.merge(date.getRecordDate()) );
    }
}
